<?php
global $_W, $_GPC;
load()->func('file');
$files = file_tree(MODULE_ROOT . '/xlsdocs/weid-' . $_W['uniacid'] . '');
$doc = $files;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ('post' == $op) {
	$id = intval($_GPC['id']);
	if (!empty($id)) {
		$item = pdo_fetch("SELECT * FROM " . tablename($this->tablecaiji) . " WHERE id = :id", array(':id' => $id));
		if (empty($item)) {
			message('数据不存在！', '', 'error');
		}
	}
	if (checksubmit('submit')) {
		$title = trim($_GPC['title']);
		if (empty($title)) {
			message('商品名称不能为空!');
		}
		$data = array('uniacid' => $_W['uniacid'], 'title' => $title, 'status' => $_GPC['status'], 'displayorder' => $_GPC['displayorder'],);
		if (empty($id)) {
			pdo_insert($this->modulename . '_cate', $data);
			message('商品添加成功！', $this->createWebUrl('listcaiji', array('op' => 'display')), 'success');
		} else {
			pdo_update($this->modulename . '_cate', $data, array('id' => $id));
			message('商品更新成功！', $this->createWebUrl('listcaiji', array('op' => 'display')), 'success');
		}
	}
} else if ('del' == $op) {
	$id = intval($_GPC['id']);
	$row = pdo_fetch("SELECT id FROM " . tablename($this->tablecaiji) . " WHERE id = :id", array(':id' => $id));
	if (empty($row)) {
		message('商品不存在！');
	}
	pdo_delete($this->modulename . '_caiji', array('id' => $id));
	message('删除成功！', referer(), 'success');
} else if ('delall' == $op) {
	$ids = $_GPC['ids'];
	foreach ($ids as $id) {
		pdo_delete($this->modulename . '_caiji', array('id' => $id));
	}
	message('删除' . $id . '成功！', referer(), 'success');
} else if ('addall' == $op) {
	$ids = $_GPC['ids'];
	$cateid = $_GPC['cateid'];
	foreach ($ids as $id) {
		$caiji = pdo_fetch('select * from ' . tablename('bsht_tbk_caiji') . " where id = '{$id}' and uniacid = '{$_W['uniacid']}' limit 1");
		$item = pdo_fetch('select * from ' . tablename('bsht_tbk_item') . " where itemid = '{$caiji['itemid']}' and uniacid = '{$_W['uniacid']}' limit 1");
		if (empty($item)) {
			$sql = "INSERT INTO " . tablename('bsht_tbk_item') . " (uniacid,displayorder,itemid,title,itempic,shoptitle,itemfee,itemmsell,itemstatus,stime,etime,itemsurl,itemurl,itemtkl,itemyhj_zl,itemyhj_yl,itemyhj_tit,itemyhj_stime,itemyhj_etime,itemyhj_url,itemyhj_tkl,status) SELECT uniacid,displayorder,itemid,title,itempic,shoptitle,itemfee,itemmsell,itemstatus,stime,etime,itemsurl,itemurl,itemtkl,itemyhj_zl,itemyhj_yl,itemyhj_tit,itemyhj_stime,itemyhj_etime,itemyhj_url,itemyhj_tkl,status FROM " . tablename('bsht_tbk_caiji') . " WHERE id='{$id}'";
			pdo_query($sql);
			$newid = pdo_insertid();
			$data = array('cate_id' => $cateid, 'atime' => TIMESTAMP,);
			pdo_update($this->tableitem, $data, array('id' => $newid));
			pdo_delete($this->modulename . '_caiji', array('id' => $id));
		} else {
			pdo_delete($this->modulename . '_caiji', array('id' => $id));
		}
	}
	message('导入成功，如未全部导入，可能有重复产品！', referer(), 'success');
} else if ('display' == $op) {
	if (checksubmit('submit')) {
		$displayorders = $_GPC['displayorder'];
		foreach ($displayorders as $id => $displayorder) {
			pdo_update($this->modulename . '_item', array('displayorder' => $displayorder), array('id' => $id));
		}
		message('批量更改排序成功', referer(), 'success');
	}
	$cate = pdo_fetchall("SELECT * FROM " . tablename($this->tablecate) . " WHERE uniacid = '{$_W['uniacid']}' order by displayorder desc");
	$pindex = max(1, intval($_GPC['page']));
	$psize = 200;
	$condition = '';
	$list = pdo_fetchall("SELECT * FROM " . tablename($this->tablecaiji) . " WHERE uniacid = '{$_W['uniacid']}' order by displayorder desc  LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->tablecaiji) . " WHERE uniacid = '{$_W['uniacid']}'");
	$pager = pagination($total, $pindex, $psize);
} else if ('status' == $op) {
	$id = $_GPC['id'];
	pdo_query('update ' . tablename($this->tablecaiji) . " set status = !status where id='{$id}'");
	die('1');
}
include $this->template('listdoc');